﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace ElectronicCommerce.Framework.Web.UI.Controls
{
    public class PageFootView : ServerControlBase
    {
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder footInfo = new StringBuilder();
            if (base.PageFoot != null)
            {
                footInfo.Append(BuildJavascript(base.PageFoot.GetJavascriptFiles()));
                footInfo.Append(BuildJavascript(base.PageFoot.GetInlineJavascripts()));
            }
            writer.WriteLine(footInfo.ToString());
        }

        private string BuildJavascript(List<JavascriptItem> javascriptItemList)
        {
            StringBuilder sb = new StringBuilder();
            if (javascriptItemList != null && javascriptItemList.Count != 0)
            {
                foreach (JavascriptItem javascriptItem in javascriptItemList)
                {
                    sb.Append("\r\t");

                    if (javascriptItem.Type == JavascriptType.Inline)
                    {
                        sb.Append(javascriptItem.Content);
                    }
                    else if (javascriptItem.Type == JavascriptType.External)
                    {
                        sb.Append("<script type=\"text/javascript\" src=\"");
                        sb.Append(base.Resource.BuildJs(javascriptItem.Content));
                        sb.Append("\"></script>");
                    }
                    else if (javascriptItem.Type == JavascriptType.ExternalOtherWeb)
                    {
                        sb.Append("<script type=\"text/javascript\" src=\"");
                        sb.Append(javascriptItem.Content);
                        sb.Append("\"></script>");
                    }
                }
            }
            return sb.ToString();
        }
    }
}
